package com.february.todo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.february.leftslidelib.CustomLinearLayout;
import com.february.leftslidelib.CustomTextView;
import com.february.leftslidelib.DPIUtil;
import com.february.leftslidelib.LeftSlideView;
import com.february.todo.DataBase.DataBaseOperate;
import com.february.todo.MyCalendar.SimpleTodoListAdapter;
import com.february.todo.tools.Bean_todo;
import com.february.todo.tools.TimeComparator;

import java.util.List;

public class TodoListAdapter extends RecyclerView.Adapter implements View.OnLongClickListener {

    private final TodoListActivity todoListActivity;
    private final Context mContext;
    private final List<Bean_todo> todoList;
    private final RecyclerView mRecyclerView;
    private LeftSlideView mLeftSlideView;

    private SimpleTodoListAdapter.RecyclerViewOnItemLongClickListener onItemLongClickListener;

    /*设置长按事件*/
    public void setOnItemLongClickListener(SimpleTodoListAdapter.RecyclerViewOnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    @Override
    public boolean onLongClick(View v) {
        int t= (Integer)v.getTag();
        return onItemLongClickListener != null && onItemLongClickListener.onItemLongClickListener(v, (Integer) v.getTag());
    }

    public interface RecyclerViewOnItemLongClickListener {
        boolean onItemLongClickListener(View view, int position);
    }

    public TodoListAdapter(TodoListActivity todoListActivity, Context context, RecyclerView recyclerView, List<Bean_todo> todoList) {
        this.todoListActivity = todoListActivity;
        this.mContext = context;
        this.mRecyclerView = recyclerView;
        this.todoList = todoList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final LeftSlideView leftSlideView = new LeftSlideView(mContext);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DPIUtil.dip2px(100.f));
        leftSlideView.setLayoutParams(params);
        LayoutInflater inflater = LayoutInflater.from(mContext);

        @SuppressLint("InflateParams") View contentView = inflater.inflate(R.layout.recyclerview_item, null);
        @SuppressLint("InflateParams") View menuView = inflater.inflate(R.layout.recyclerview_item_menu, null);

        leftSlideView.addContentView(contentView);
        leftSlideView.addMenuView(menuView);
        leftSlideView.setRecyclerView(mRecyclerView);
        leftSlideView.setStatusChangeLister(new LeftSlideView.OnDelViewStatusChangeLister() {
            @Override
            public void onStatusChange(boolean show) {
                if (show) {
                    // 如果编辑菜单在显示
                    mLeftSlideView = leftSlideView;
                }
            }
        });

        final TodoListActivity.MyVH holder = new TodoListActivity.MyVH(leftSlideView);
        menuView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CheckBox checkBox = holder.itemView.findViewById(R.id.checkBox);
                TextView tv_menu = holder.itemView.findViewById(R.id.btn_menu);
                CustomTextView tv_title = holder.itemView.findViewById(R.id.tv_title);
                TextView tv_timeStart = holder.itemView.findViewById(R.id.tv_timeStart);
                TextView tv_date = holder.itemView.findViewById(R.id.tv_date);
                Bean_todo todo = todoList.get(holder.getAdapterPosition());
                if (checkBox.isChecked()) {
                    checkBox.setChecked(false);
                    tv_menu.setText("完成");
                    tv_menu.setBackgroundColor(0xFF7FCB7A);
                    if (todo.getState() == 1) {
                        tv_title.setTextColor(0xFFFE6363);
                        tv_date.setTextColor(0xFFFE6363);
                        tv_timeStart.setTextColor(0xFFFE6363);
                    }
                    else if(todo.getState() == 2){
                        tv_title.setTextColor(0xFF7D7D7D);
                        tv_date.setTextColor(0xFF7D7D7D);
                        tv_timeStart.setTextColor(0xFF7D7D7D);
                    }

                } else {
                    checkBox.setChecked(true);
                    tv_menu.setText("取消");
                    tv_menu.setBackgroundColor(0xFFFE6363);
                    tv_title.setTextColor(0xFF7D7D7D);
                    tv_date.setTextColor(0xFF7D7D7D);
                    tv_timeStart.setTextColor(0xFF7D7D7D);
                }
            }
        });

        contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Bean_todo todo = todoList.get(position);
        CustomTextView tv_title = holder.itemView.findViewById(R.id.tv_title);
        TextView tv_timeStart = holder.itemView.findViewById(R.id.tv_timeStart);
        TextView tv_date = holder.itemView.findViewById(R.id.tv_date);
        TextView tv_menu = holder.itemView.findViewById(R.id.btn_menu);
        CheckBox checkBox = holder.itemView.findViewById(R.id.checkBox);
        ImageButton delete = holder.itemView.findViewById(R.id.imgbtn_delete);
        CustomLinearLayout customLinearLayout = holder.itemView.findViewById(R.id.customLine_background);
        customLinearLayout.setTag(position);

        tv_title.setText(todo.getTitle());
        tv_timeStart.setText(todo.getTimeStart());
        tv_date.setText(todo.getDate());

        if (todo.getState() == 1) {
            tv_title.setTextColor(0xFFFE6363);
            tv_date.setTextColor(0xFFFE6363);
            tv_timeStart.setTextColor(0xFFFE6363);
            checkBox.setChecked(false);
        }else if (todo.getState() == 2) {
            tv_title.setTextColor(0xFF7D7D7D);
            tv_date.setTextColor(0xFF7D7D7D);
            tv_timeStart.setTextColor(0xFF7D7D7D);
            checkBox.setChecked(true);
            tv_menu.setText("取消");
            tv_menu.setBackgroundColor(0xFFFE6363);
        } else {
            tv_title.setTextColor(0xFF7D7D7D);
            tv_date.setTextColor(0xFF7D7D7D);
            tv_timeStart.setTextColor(0xFF7D7D7D);
            checkBox.setChecked(false);
        }

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                DataBaseOperate dataBaseOperate = new DataBaseOperate(todoListActivity);
                if (isChecked) {
                    dataBaseOperate.setFinished(1, todo.getId());
                    todo.setState(2);
                    checkBox.setChecked(true);
                    tv_menu.setText("取消");
                    tv_title.setTextColor(0xFF7D7D7D);
                    tv_date.setTextColor(0xFF7D7D7D);
                    tv_timeStart.setTextColor(0xFF7D7D7D);
                } else {
                    dataBaseOperate.setFinished(0, todo.getId());
                    if (new TimeComparator(todo.getDate(), todo.getTimeEnd()).isEarlier) {
                        todo.setState(1);
                        tv_title.setTextColor(0xFFFE6363);
                        tv_date.setTextColor(0xFFFE6363);
                        tv_timeStart.setTextColor(0xFFFE6363);
                    } else {
                        todo.setState(0);
                        tv_title.setTextColor(0xFF7D7D7D);
                        tv_date.setTextColor(0xFF7D7D7D);
                        tv_timeStart.setTextColor(0xFF7D7D7D);
                    }
                    tv_menu.setText("完成");
                    tv_menu.setBackgroundColor(0xFF7FCB7A);
                }
            }
        });

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(todoListActivity);
                builder.setIcon(null);
                builder.setTitle("提  示");
                builder.setMessage("确定要删除吗？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        DataBaseOperate dataBaseOperate = new DataBaseOperate(todoListActivity);
                        dataBaseOperate.deleteTodo(todo.getId());
                        int pos = holder.getAdapterPosition();
                        todoList.remove(pos);
                        notifyItemRemoved(pos);
                        notifyItemChanged(0, todoList.size());
                        dialogInterface.dismiss();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                }).show();
            }
        });

        customLinearLayout.setOnLongClickListener(this);
    }

    @Override
    public int getItemCount() {
        return todoList.size();
    }

    /**
     * 还原itemView
     *
     * @param point
     */
    public void restoreItemView(Point point) {
        if (mLeftSlideView != null) {

            int[] pos = new int[2];

            mLeftSlideView.getLocationInWindow(pos);

            int width = mLeftSlideView.getWidth();
            int height = mLeftSlideView.getHeight();

            // 触摸点在view的区域内，那么直接返回
            if (point.x >= pos[0] && point.y >= pos[1]
                    && point.x <= pos[0] + width && point.y <= pos[1] + height) {
                return;
            }
            mLeftSlideView.resetDelStatus();
        }
    }
}
